package com.xoa.controller.party;


import com.xoa.model.party.InformationDelivery;
import com.xoa.service.party.InformationDeliveryService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/InformationDelivery")
public class InformationDeliveryController {

    @Autowired
    private InformationDeliveryService informationDeliveryService;
    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息报送首页映射
     */
    @RequestMapping("/infoIndex")
    public String infoIndex(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoIndex";
    }

    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息报送首页映射
     */
    @RequestMapping("/infoList")
    public String infoList(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoList";
    }

    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息报送表单映射
     */
    @RequestMapping("/infoForm")
    public String infoForm(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoForm";
    }

    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息查询首页映射
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoQuery/index";
    }

    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息查询映射
     */
    @RequestMapping("/infoQuery")
    public String infoQuery(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoQuery/infoQuery";
    }

    /**
     * 创建作者:   杨璞
     * 创建日期:   2017年9月18日
     * 方法介绍:   页面跳转连接
     * @return  信息查询映射
     */
    @RequestMapping("/infoStatistics")
    public String infoStatistics(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/InformationDelivery/infoQuery/infoStatistics";
    }


    /**
     * 查询所属社区信息
     * @param request
     * @return
     */
    @RequestMapping("/selectInFo")
    @ResponseBody
    public ToJson selectInFo (HttpServletRequest request, Integer page, Integer pageSize, boolean useFlag){
            return  informationDeliveryService.selectInFo(request,page,pageSize,useFlag);
    }

    /**
     * 新建信息报送前生成
     * @param request
     * @param
     * @return
     * @return
     */
    @RequestMapping("/Insertinto")
    @ResponseBody
    public ToJson Insertinto(HttpServletRequest request){
             return informationDeliveryService.Insertinto(request);
    }

    /**
     * 新建信息报送
     */
    @RequestMapping("/inserttrinto")
    @ResponseBody
    public ToJson insertTO(HttpServletRequest request, InformationDelivery informationDelivery, String Timemeeting, String Timedelivery){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            informationDelivery.setMeetingTime(formatter.parse(Timemeeting));
            informationDelivery.setDeliveryTime(formatter.parse(Timedelivery));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  informationDeliveryService.InsetInfro(request,informationDelivery);
    }

    /**
     * 查看数据/导出pdf
     * @param infoid
     * @return
     */
    @RequestMapping("/updateinser")
    @ResponseBody
    public ToJson updateinser(HttpServletRequest request, Integer infoid){
            return  informationDeliveryService.updateinser(request,infoid);
    }

    /**
     * 编辑
     * @param request
     * @param informationDelivery
     * @return
     */
    @RequestMapping("/updateinfo")
    @ResponseBody
    public ToJson updateinfo(HttpServletRequest request, InformationDelivery informationDelivery, String Timemeeting, String Timedelivery){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            informationDelivery.setMeetingTime(formatter.parse(Timemeeting));
            informationDelivery.setDeliveryTime(formatter.parse(Timedelivery));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  informationDeliveryService.updateinfo(request,informationDelivery);
    }

    /**
     * 删除
     * @param request
     * @param infoid
     * @return
     */
    @RequestMapping("/deleteinfo")
    @ResponseBody
    public ToJson deleteinfo(HttpServletRequest request, Integer infoid){
        return informationDeliveryService.deleteinfo(request,infoid);
    }

    /**
     * 根据条件查询/导出
     * @param request
     * @param informationDelivery
     * @return
     */
    @RequestMapping("/selectInfoto")
    @ResponseBody
    public ToJson selectInfoto(HttpServletRequest request, HttpServletResponse response, InformationDelivery informationDelivery, String startTime, String endTime, Integer page, Integer pageSize, boolean useFlag){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            informationDelivery.setStartTime(startTime);
            informationDelivery.setEndTime(endTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        return informationDeliveryService.selectInfoto(request,response,informationDelivery,page,pageSize,useFlag);
    }

    /**
     * 统计数量
     * @return
     */
    @RequestMapping("/selectInfo")
    @ResponseBody
    public ToJson selectInfo(Integer year){
        return informationDeliveryService.selectInfo(year);
    }

}
